package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.ism.constant.SignatureConstant;
import com.sec.etech.ism.service.IsmCommonService;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmTaskService;
import lombok.extern.slf4j.Slf4j;
import org.openbpm.base.api.exception.BusinessMessage;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class IsmSendComponentBomServiceImpl implements IsmMethodService {
    @Resource
    IsmTaskService ismTaskService;
    @Resource
    IsmCommonService ismCommonService;

    public IsmSendComponentBomServiceImpl() {
    }

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject businessParams) throws Exception {
        JSONObject result = new JSONObject();
        JSONObject object = this.checkAndGetData(businessParams);
        JSONObject resData = ismCommonService.sendComponentBom(object.getString("material_no"), object.getString("componentNum"));
        ismTaskService.deleteTaskByParams(sysCode, serviceCode, serviceCode, null,
                businessParams.getString(SignatureConstant.INPUT_PARAMS));
        result.put("flag", "success");
        result.put("data", resData);
        return result;
    }

    //入参检查
    private JSONObject checkAndGetData(JSONObject busParams) throws BusinessMessage {
        JSONObject object = busParams.getJSONObject(SignatureConstant.INPUT_PARAMS);
        String materialNo = object.getString("material_no");
        String componentNum = object.getString("componentNum");
        if (StringUtil.isEmpty(materialNo) && StringUtil.isEmpty(componentNum)) {
            throw new BusinessMessage("请求中material_no、componentNum参数不可全部为空，请检查！");
        }
        return object;
    }

    @Override
    public String methodName() {
        return "sendComponentBom";
    }
}
